package com.comtime.greendaoutils;

import java.util.List;

import android.content.Context;
import android.util.Log;

import com.comtime.greendaodemo.AdminDao;
import com.comtime.greendaodemo.AdminInfo;
import com.comtime.greendaodemo.AuthInfo;
import com.comtime.greendaodemo.AuthInfoDao;
import com.comtime.greendaodemo.DaoMaster;
import com.comtime.greendaodemo.DaoMaster.OpenHelper;
import com.comtime.greendaodemo.DaoSession;
import com.comtime.greendaodemo.DeviceInfo;
import com.comtime.greendaodemo.DeviceInfoDao;
import com.comtime.greendaodemo.FingerprintInfo;
import com.comtime.greendaodemo.FingerprintInfoDao;
import com.comtime.greendaodemo.MemberInfo;
import com.comtime.greendaodemo.MemberInfoDao;
import com.comtime.greendaodemo.NewInfo;
import com.comtime.greendaodemo.NewInfoDao;
import com.comtime.greendaodemo.OpenLockInfo;
import com.comtime.greendaodemo.OpenLockInfoDao;
import com.comtime.greendaodemo.RemindInfo;
import com.comtime.greendaodemo.RemindInfoDao;
import com.comtime.utils.MyConfig;

import de.greenrobot.dao.query.QueryBuilder;

public class DataBaseUtil {

	private static DataBaseUtil instance;
	private static DaoSession mDaoSession;
	private static DaoMaster daoMaster;
	private static Context appContext;
	private NewInfoDao newInfoDao;
	private DeviceInfoDao deviceInfoDao;
	private RemindInfoDao remindInfoDao;
	private OpenLockInfoDao openLockInfoDao;
	private AdminDao adminDao;
	private MemberInfoDao memberInfoDao;
	private AuthInfoDao authInfoDao;
	private FingerprintInfoDao fingerprintInfoDao;

	private DataBaseUtil() {

	}

	public static DataBaseUtil getInstance(Context context) {
		if (instance == null) {
			instance = new DataBaseUtil();
			if (appContext == null) {
				appContext = context.getApplicationContext();
			}
			OpenHelper helper = new DaoMaster.DevOpenHelper(context,
					MyConfig.DB_NAME, null);
			daoMaster = new DaoMaster(helper.getWritableDatabase());
			mDaoSession = daoMaster.newSession();

			instance.newInfoDao = mDaoSession.getNewInfoDao();
			instance.deviceInfoDao = mDaoSession.getDeviceInfoDao();
			instance.remindInfoDao = mDaoSession.getRemindInfoDao();
			instance.openLockInfoDao = mDaoSession.getOpenLockInfoDao();
			instance.adminDao = mDaoSession.getAnmindao();
			instance.memberInfoDao = mDaoSession.getMemberInfoDao();
			instance.authInfoDao= mDaoSession.getAuthInfoDao();
			instance.fingerprintInfoDao=mDaoSession.getFingerprintInfoDao();
		}
		return instance;

	}

	
	public List<FingerprintInfo>  getFingerprintInfosByMac(String mac){
		QueryBuilder<FingerprintInfo> qb = fingerprintInfoDao.queryBuilder();
		qb.where( com.comtime.greendaodemo.FingerprintInfoDao.Properties.Mac.eq(mac) );
		if (qb.list().size() > 0) {
			return qb.list();
		}
		return null;
	}
	
	public void saveFingerprintInfo(FingerprintInfo info){
		fingerprintInfoDao.insertOrReplace(info);
	}
	
	
	public List<AuthInfo>  getAuthInfosByPhoneNum(String phone){
		QueryBuilder<AuthInfo> qb = authInfoDao.queryBuilder();
		qb.where( com.comtime.greendaodemo.AuthInfoDao.Properties.PhoneNum.eq(phone) );
		if (qb.list().size() > 0) {
			return qb.list();
		}
		return null;
	}
	
	public void saveAuthInfo(AuthInfo authInfo){
		authInfoDao.insertOrReplace(authInfo);
	}
	
//	public void deleteAuthInfoByMac(String  mac){
//		 
//		QueryBuilder<AuthInfo> qb = authInfoDao.queryBuilder();
//		qb.where(com.comtime.greendaodemo.AuthInfoDao.Properties.Mac
//				.eq(mac));
//		if (qb.list().size() > 0) {
//			List<AuthInfo> infos = qb.list();
//
//			for (int i = 0; i < infos.size(); i++) {
//
//				AuthInfo info = infos.get(i);
//				authInfoDao.delete(info);
//			}
//		}
//	}
	
	public void deleteAuthInfo(AuthInfo authInfo){
		authInfoDao.delete(authInfo);
	}
	
	
	public void deleteAllDeviceInfo() {
		deviceInfoDao.deleteAll();
	}
	public void deleteDeviceInfo(DeviceInfo deviceInfo) {
		deviceInfoDao.delete(deviceInfo);
	}
	/**
	 * save
	 * 
	 * @param newInfo
	 * @return
	 */
	public long saveNewInfo(NewInfo newInfo) {
		return newInfoDao.insertOrReplace(newInfo);
	}

	public List<NewInfo> loadNewInfos() {
		return newInfoDao.loadAll();

	}

	/**
	 * 绑定设备信息
	 */

	public long saveDeviceInfo(DeviceInfo deviceInfo) {
		return deviceInfoDao.insertOrReplace(deviceInfo);
	}

	public void deleteDeviceInfo(long id) {
		QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.Id.eq(""
				+ id));
		if (qb.list().size() > 0) {
			List<DeviceInfo> infos = qb.list();

			for (int i = 0; i < infos.size(); i++) {

				DeviceInfo info = infos.get(i);
				deviceInfoDao.delete(info);
			}
		}
	}

	public void deleteDeviceInfoByPhoneNum(String phonenum) {
		QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.PhoneNum
				.eq("" + phonenum));
		if (qb.list().size() > 0) {
			List<DeviceInfo> infos = qb.list();

			for (int i = 0; i < infos.size(); i++) {

				DeviceInfo info = infos.get(i);
				deviceInfoDao.delete(info);
			}
		}
	}

	public void deleteDeviceInfo(String phonenum, String mac) {
		QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.PhoneNum
				.eq("" + phonenum),
				com.comtime.greendaodemo.DeviceInfoDao.Properties.Mac.eq(""
						+ mac));
		if (qb.list().size() > 0) {
			List<DeviceInfo> infos = qb.list();

			for (int i = 0; i < infos.size(); i++) {

				DeviceInfo info = infos.get(i);
				deviceInfoDao.delete(info);
			}
		}
	}

	public List<DeviceInfo> loadDeviceInfo(String phonenum) {
		QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.PhoneNum
				.eq("" + phonenum));
		if (qb.list().size() > 0) {
			return qb.list();

		}

		return null;

	}

	public DeviceInfo loadDeviceInfoByMac(String phoneNum, String mac) {
		QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.PhoneNum
				.eq("" + phoneNum),
				com.comtime.greendaodemo.DeviceInfoDao.Properties.Mac.eq(""
						+ mac));
		if (qb.list().size() > 0) {
			return qb.list().get(0);

		}
		return null;
	}

    public List<DeviceInfo> showDeviceInfoByMac(String mac) {
        QueryBuilder<DeviceInfo> qb = deviceInfoDao.queryBuilder();
        qb.where(com.comtime.greendaodemo.DeviceInfoDao.Properties.Mac.eq(""
                        + mac));
        if (qb.list().size() > 0) {
            return qb.list();

        }
        return null;
    }

	/******************************************************************/
	/**
	 * 提醒信息
	 */
	public long saveRemindInfo(RemindInfo remindInfo) {
		return remindInfoDao.insertOrReplace(remindInfo);

	}

	public void deleteRemindInfo(long id) {
		QueryBuilder<RemindInfo> qb = remindInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.RemindInfoDao.Properties.Id.eq(""
				+ id));
		if (qb.list().size() > 0) {
			List<RemindInfo> infos = qb.list();

			for (int i = 0; i < infos.size(); i++) {

				RemindInfo info = infos.get(i);
				remindInfoDao.delete(info);
			}
		}

	}

	
	/**
	 * 获取显示提醒信息
	 * 
	 * @param
	 * @param
	 * @return
	 */
	public List<RemindInfo> loadRemindInfoByShow(int user_id, String mac) {
		QueryBuilder<RemindInfo> qb = remindInfoDao.queryBuilder();
		qb.where(
				com.comtime.greendaodemo.RemindInfoDao.Properties.User_id.eq(""
						+ user_id),
				com.comtime.greendaodemo.RemindInfoDao.Properties.MacAddress
						.eq("" + mac),com.comtime.greendaodemo.RemindInfoDao.Properties.Upflag.notEq(""+2)).orderDesc(
				com.comtime.greendaodemo.RemindInfoDao.Properties.Id);
		if (qb.list().size() > 0) {
			return qb.list();
		}
		return null;
	}
	
	
	/**
	 * 获取要上传提醒信息
	 * 
	 * @param
	 * @param
	 * @return
	 */
	public List<RemindInfo> loadRemindInfoByUpflag(int user_id, int upflag) {
		QueryBuilder<RemindInfo> qb = remindInfoDao.queryBuilder();
		qb.where(
				com.comtime.greendaodemo.RemindInfoDao.Properties.User_id.eq(""
						+ user_id),
				com.comtime.greendaodemo.RemindInfoDao.Properties.Upflag.eq(""
						+ upflag)).orderDesc(
				com.comtime.greendaodemo.RemindInfoDao.Properties.Id);
		if (qb.list().size() > 0) {
			return qb.list();
		}
		return null;
	}

	/********************************** 开锁记录 *******************************************/
	public long saveOpenLockInfo(OpenLockInfo openLockInfo) {
		return openLockInfoDao.insertOrReplace(openLockInfo);
	}

	public void deleteOpenLockInfo(OpenLockInfo openLockInfo) {
		openLockInfoDao.delete(openLockInfo);
	}
	/**
	 * 获取指定开锁记录
	 * @param user_id
	 * @param flag
	 * @return
	 */
	public List<OpenLockInfo> loadOpenLockInfoByUpflag(int user_id, int flag) {
		QueryBuilder<OpenLockInfo> qb = openLockInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.OpenLockInfoDao.Properties.User_id
				.eq("" + user_id),
				com.comtime.greendaodemo.OpenLockInfoDao.Properties.Upflag
						.eq("" + flag)).orderDesc(
								com.comtime.greendaodemo.OpenLockInfoDao.Properties.Id);
		if (qb.list().size() > 0) {

			return qb.list();
		} else {
			return null;
		}

	}
	/**
	 * 获取显示开锁记录
	 * @param user_id
	 * @param flag
	 * @return
	 */
	public List<OpenLockInfo> loadOpenLockInfoByUpshow(int user_id,String mac) {
		QueryBuilder<OpenLockInfo> qb = openLockInfoDao.queryBuilder();
		qb.where(com.comtime.greendaodemo.OpenLockInfoDao.Properties.User_id
				.eq("" + user_id),com.comtime.greendaodemo.OpenLockInfoDao.Properties.MacAddress.eq(mac),
				com.comtime.greendaodemo.OpenLockInfoDao.Properties.Upflag
						.notEq("" + 2)).orderDesc(
								com.comtime.greendaodemo.OpenLockInfoDao.Properties.Id);
		if (qb.list().size() > 0) {

			return qb.list();
		} else {
			return null;
		}

	}

	/********************************** 管理员信息 *******************************************/
	public long saveAdminInfo(AdminInfo adminInfo) {
		return adminDao.insertOrReplace(adminInfo);
	}

	public void deleteAdminInfo(AdminInfo adminInfo) {
		adminDao.delete(adminInfo);

	}


	/**
	 * @param mac
	 * @param phone
	 * @return
	 */
	public List<AdminInfo> loadAdminInfoByUpShow(String phone,String mac) {
		QueryBuilder<AdminInfo> qb = adminDao.queryBuilder();
		qb.where(AdminDao.Properties.deviceMac
						.eq(mac), AdminDao.Properties.aPhoneNum.eq(phone)).orderDesc(
				AdminDao.Properties.Id);
		if (qb.list().size() > 0) {
			return qb.list();
		} else {
			return null;
		}

	}

    public List<AdminInfo> ShowAdminInfoByPhone(String phone) {
        QueryBuilder<AdminInfo> qb = adminDao.queryBuilder();
        qb.where(AdminDao.Properties.aPhoneNum.eq(phone)).orderDesc(
				AdminDao.Properties.Id);
        if (qb.list().size() > 0) {

            return qb.list();
        } else {
            return null;
        }

    }

    /*public boolean isAdmin(String phone , String mac){
		QueryBuilder<AdminInfo> qb = adminDao.queryBuilder();
		qb.where()
		return false;
	}*/

	/********************************** 成员用户信息 *******************************************/
	public long saveMemberInfo(MemberInfo memberInfo) {
		return memberInfoDao.insertOrReplace(memberInfo);
	}

	public void deleteMemberInfo(MemberInfo memberInfo) {
		memberInfoDao.delete(memberInfo);

	}



	/**
	 * @param mac
	 * @param phone
	 * @return
	 */
	public List<MemberInfo> loadMemberInfoByUpShow(String phone,String mac) {
		QueryBuilder<MemberInfo> qb = memberInfoDao.queryBuilder();
		qb.where(MemberInfoDao.Properties.memberPhone
				.eq(phone), MemberInfoDao.Properties.memberMac.eq(mac));
		if (qb.list().size() > 0) {
			return qb.list();
		} else {
			return null;
		}

	}

	public List<MemberInfo> ShowMemberInfoByMac(String mac) {
		QueryBuilder<MemberInfo> qb = memberInfoDao.queryBuilder();
		qb.where(MemberInfoDao.Properties.memberMac.eq(mac));
		if (qb.list().size() > 0) {
			return qb.list();
		} else {
			return null;
		}

	}
}
